package com.zmn.plat.admin.controller.channel.tariff;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.model.entity.channel.tariff.ChannelTariff;
import com.zmn.plat.model.entity.service.tariff.ServiceTariff;

/**
 * 价格表使用渠道关联表
 *
 * @author linfeng
 *
 * @since 2020-03-03 10:10:14
 */
@RestController
@RequestMapping("/channel/tariff/")
public class ChannelTariffModifyController extends ChannelTariffBaseController {

    /**
     * 新增价格表与渠道之前的使用关系
     * 
     * @author linfeng
     * @param channelIds
     * @since 2020-03-03 10:10:14
     */
    @RequestMapping(value = "add")
    @RequiresPermissions("/channel/tariff/add.action")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestParam(value = "ids", defaultValue = "0") String channelIds,
        @RequestParam(value = "tariffId", defaultValue = "0") Integer tariffId) {
        if (StringUtils.isEmpty(channelIds)) {
            return ResponseDTO.fail("请选择要新增的数据！");
        }

        if (tariffId == null || tariffId == 0) {
            return ResponseDTO.fail("价格表ID不能为空！");
        }

        Date now = DateUtil.getNow();
        String creater = getMcStaffRealName();
        List<String> list = Arrays.asList(channelIds.split(","));
        ServiceTariff faultTariff = faultTariffService.findByKey(tariffId);
        channelTariffService.deleteByChannelIdsAndBizTypeAndTariffType(list, faultTariff.getBizType(), faultTariff.getTariffType());

        List<ChannelTariff> channelTariffs = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ChannelTariff channelTariff = new ChannelTariff();
            channelTariff.setChannelId(Integer.valueOf(list.get(i)));
            channelTariff.setTariffId(tariffId);
            channelTariff.setCreater(creater);
            channelTariff.setCreateTime(now);
            channelTariff.setUpdater(creater);
            channelTariff.setUpdateTime(now);
            channelTariffs.add(channelTariff);
        }

        Integer result = channelTariffService.insertBatch(channelTariffs);

        return ResponseDTO.success(result, "操作成功！");
    }

    @RequestMapping(value = "delete")
    @RequiresPermissions("/channel/tariff/delete.action")
    public ResponseDTO delete(@RequestParam(value = "ids", defaultValue = "0") String ids) {
        if (StringUtils.isEmpty(ids)) {
            return ResponseDTO.fail("请选择要删除的数据！");
        }

        List<String> list = Arrays.asList(ids.split(","));
        Integer result = channelTariffService.deleteByKeys(list);

        return ResponseDTO.success(result, "操作成功！");
    }
}
